package org.pine.dictionary.controller;


import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.pine.dictionary.bean.*;
import org.pine.dictionary.bean.request.DictionaryQueryRequest;
import org.pine.dictionary.bean.request.DictionarySaveRequest;
import org.pine.dictionary.bean.response.DictionaryQueryResponse;
import org.pine.dictionary.service.IDictionaryService;
import org.pine.dictionary.util.ResultUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author pine
 * @since 2024-05-15
 */
@Tag(name = "字典表 前端控制器")
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Resource
    private IDictionaryService dictionaryService;

    /**
     * 查询字典列表（分页）
     */
    @Operation(summary = "查询字典列表（分页）")
    @PostMapping("/list")
    public ResultVo<PageResponse<DictionaryQueryResponse>> list(@Valid @RequestBody PageRequest<DictionaryQueryRequest> request) {
        request.defaultPage();
        PageResponse<DictionaryQueryResponse> pageResponse = dictionaryService.list(request);
        return ResultUtil.success(pageResponse);
    }

    /**
     * 获取单个字典信息
     */
    @PostMapping(value = "/getDetail")
    @Operation(summary = "获取单个字典信息")
    public ResultVo<DictionaryQueryResponse> getDetail(@Valid @RequestBody IdRequest<String> request) {
        DictionaryQueryResponse queryResponse = dictionaryService.getDetail(request.getId());
        return ResultUtil.success(queryResponse);
    }

    /**
     * 新增字典信息
     */
    @PostMapping(value = "/save")
    @Operation(summary = "新增字典信息")
    public ResultVo<Void> save(@Valid @RequestBody DictionarySaveRequest request) {
        dictionaryService.insert(request);
        return ResultUtil.success(null);
    }

    /**
     * 修改字典信息
     */
    @PostMapping(value = "/update")
    @Operation(summary = "修改字典信息")
    public ResultVo<Void> update(@Valid @RequestBody DictionarySaveRequest request) {
        dictionaryService.update(request);
        return ResultUtil.success(null);
    }

    /**
     * 删除字典信息
     */
    @PostMapping(value = "/delete")
    @Operation(summary = "删除字典信息")
    public ResultVo<Void> delete(@Valid @RequestBody BatchIdRequest<String> request) {
        dictionaryService.delete(request.getIdList());
        return ResultUtil.success(null);
    }

    /**
     * 启用字典信息
     */
    @PostMapping(value = "/enable")
    @Operation(summary = "启用字典信息")
    public ResultVo<Void> enable(@Valid @RequestBody BatchIdRequest<String> request) {
        dictionaryService.enable(request.getIdList());
        return ResultUtil.success(null);
    }


    /**
     * 禁用字典信息
     */
    @PostMapping(value = "/disable")
    @Operation(summary = "禁用字典信息")
    public ResultVo<Void> disable(@Valid @RequestBody BatchIdRequest<String> request) {
        dictionaryService.disable(request.getIdList());
        return ResultUtil.success(null);
    }

    /**
     * 导入字典
     */
    @PostMapping("/import")
    @Operation(summary = "导入字典")
    public ResultVo<Void> importExcel(@RequestPart("file") MultipartFile file) {
        dictionaryService.importExcel(file);
        return ResultUtil.success(null);
    }

    /**
     * 字典导入模版
     */
    @GetMapping("/importTpl")
    @Operation(summary = "字典导入模版")
    public void importTpl(HttpServletResponse response) throws IOException {
        try {
            dictionaryService.importTpl(response);
        } catch (Exception exception) {
            // 重置response
            response.reset();
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            response.setCharacterEncoding("utf-8");
            ResultVo<Boolean> resultVo = ResultUtil.error("1001", "获取导入模版错误");
            response.getWriter().println(JSONUtil.toJsonStr(resultVo));
        }
    }

    /**
     * 导出字典
     */
    @PostMapping("/export")
    @Operation(summary = "导出字典")
    public ResultVo<Void> exportExcel(HttpServletResponse response, @RequestBody @Valid DictionaryQueryRequest request) throws IOException {
        dictionaryService.export(response, request);
        return ResultUtil.success(null);
    }
}
